import java.util.Comparator;
import java.util.PriorityQueue;

class Intcmp implements Comparator<Integer> {
    @Override
    public int compare(Integer i1, Integer i2) {
        return i2-i1;
    }
}
public class Solution {
    public int[] smallestK(int[] arr, int k) {
        if(arr==null || k<=0){
            return new int[0];
        }
        PriorityQueue<Integer> q = new PriorityQueue<>(new Intcmp());
        for (int i = 0; i < k; i++) {
            q.offer(arr[i]);
        }
        for (int i = k; i < arr.length; i++) {
            if (arr[i] < q.peek()) {
                q.poll();  // 移除堆顶元素
                q.add(arr[i]);  // 添加新元素
            }
        }
        int[] res = new int[k];
        // 输出堆中的元素
        while (!q.isEmpty()) {
            for(int i=0;i<k;i++){
                res[i] = q.poll();
            }
        }
        return res;
    }
}
